// pages/daka/daka.js
var util = require('../../utils/util.js')
var app = getApp();
var key = app.globalData.key;
var resurl = app.globalData.resurl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isDelay: '',
    videoShow: false,
    bannerShow: true,
    showMask: '',
    fourShow: '',
    rewardNum: '',
    userMoney: '',//真钱
    money: '',//元宝数量
    taskColor:[
      'linear-gradient(0deg,rgba(248,61,49,1),rgba(255,94,84,1))',
      'linear-gradient(0deg,rgba(252,151,98,1),rgba(254,173,99,1))',
      'linear-gradient(0deg,rgba(90,192,246,1),rgba(21,168,253,1))',
  ],
    taskList: [
      {
        src:'/images/image_target_1.png',
        title:'早晚打卡',
        haveNum:'',
        needNum:'',
        content:'05:00-21:00',
        isShow:'',
        jobType:'',
        id:'',
        rewardType:'',
        url: '/pages/sleep/sleep',

      },
      {
        src:'/images/image_task_target_eye.png',
        title:'眼保健操',
        haveNum:'',
        needNum:'',
        content:'每天做眼保健操得元宝',
        isShow:'',
        jobType:'',
        id:'',
        url: '/pages/eyes/eyes',
        rewardType:'',
      },
      {
        src:'/images/image_target_2.png',
        title:'喝水打卡',
        haveNum:'',
        needNum:'',
        content:'每日健康喝水',
        isShow:'',
        jobType:'',
        id:'',
        rewardType:'',
        url: '/pages/drink/drink',
      },
      {
        src:'/images/image_target_4.png',
        title:'三餐打卡',
        haveNum:'',
        needNum:'',
        content:'每日三餐吃饭打卡',
        isShow:'',
        jobType:'',
        id:'',
        rewardType:'',
        url: '/pages/lunch/lunch',
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var isDelay = wx.getStorageSync("isDelay");
    that.setData({
      isDelay,
    })
  },
  turnPage:function (e) {
    const index= e.currentTarget.dataset.index;//获取点击event的索引
    var that = this;
    const url= e.currentTarget.dataset.url;//获取点击event的索引
    console.log(index);
    if( index === 0){
      let now = new Date();
    let timeStamp = now.getTime();
    let beginTime = function (begintime) {
      var bArray = begintime.split(":");
      if(bArray.length !== 2){
        return false;
      }
      var b = new Date();
      b.setHours(bArray[0]);
      b.setMinutes(bArray[1]);
      var btimeStamp = b.getTime();
      return btimeStamp;
    }
    let endTime = function (endtime) {
      var eArray = endtime.split(":");
      if(eArray.length !== 2) {
        return false;
      }
      var e = new Date();
      e.setHours(eArray[0]);
      e.setMinutes(eArray[1]);
      var etimeStamp = e.getTime();
      return etimeStamp;
    }
    if(timeStamp > beginTime("02:00") && timeStamp < endTime("20:00")) {
      wx.navigateTo({
        url: '/pages/morning/morning',
      })
    }else {
      wx.navigateTo({
        url: '/pages/sleep/sleep',
      })
    }
  } else {
    wx.navigateTo({
      url: url
    })
  } 
  },
  //去完成可领取已领取按钮
  tofinished:function (e) {
    const index= e.currentTarget.dataset.index;//获取点击event的索引
    var that = this;
    const url= e.currentTarget.dataset.url;//获取点击event的索引
    const taskList = e.currentTarget.dataset.tasklist;
    console.log(index);
    if( index === 0){
      let now = new Date();
    let timeStamp = now.getTime();
    let beginTime = function (begintime) {
      var bArray = begintime.split(":");
      if(bArray.length !== 2){
        return false;
      }
      var b = new Date();
      b.setHours(bArray[0]);
      b.setMinutes(bArray[1]);
      var btimeStamp = b.getTime();
      return btimeStamp;
    }
    let endTime = function (endtime) {
      var eArray = endtime.split(":");
      if(eArray.length !== 2) {
        return false;
      }
      var e = new Date();
      e.setHours(eArray[0]);
      e.setMinutes(eArray[1]);
      var etimeStamp = e.getTime();
      return etimeStamp;
    }
    if(timeStamp > beginTime("02:00") && timeStamp < endTime("20:00")) {
      wx.navigateTo({
        url: '/pages/morning/morning',
      })
    }else {
      wx.navigateTo({
        url: '/pages/sleep/sleep',
      })
    }
  } else {
    wx.navigateTo({
      url: url
    })
  } 
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //interface 接口10 打卡界面
    var that = this ;
    that.setData({
      money: app.globalData.money,
    })
    // debugger
    var userId = wx.getStorageSync("userId");
    var _data = {
      deviceProperties: app.globalData.deviceProperties,
      user:{
        userId:userId,
      }
    }
    _data = JSON.stringify(_data);
    _data = util.base64_encode(_data);
    _data = util.xxtea_encrypt(_data, key);
    wx.request({
      url:  resurl + '/interface/sdkData?requestId=10&a=1&b=2',
      data:_data,
      header:{
        "content-type":"application/json",
        "dataType":"json",
      },
      method:"post",
      success:function(res){
        res = util.xxtea_decrypt(res.data,key);
        res = util.base64_decode(res);
        res = JSON.parse(res);
        if (res.resultCode === 0) {
          res = JSON.parse(res.data);
          console.log(res);
          const tList = res.taskList;
          var taskList = that.data.taskList;
          for (var i = 0; i < tList.length; i++){
            taskList[i].haveNum = tList[i].haveNum;
            taskList[i].needNum = tList[i].needNum;
            taskList[i].id = tList[i].id;
            taskList[i].isEnd = tList[i].isEnd;
            var rewardNum = tList[i].rewardNum;
          }
          console.log(rewardNum);
          var money = that.data.money;
          that.setData({
            taskList:taskList,
            rewardNum: rewardNum,
          })
            if(tList[0].isEnd ===2) {
              var newDay0 = new Date().toLocaleDateString();
              var oldDay0 = wx.getStorageSync('oldDay0',oldDay0);
              if(oldDay0) {
                if(newDay0 != oldDay0) {
                  app.globalData.money = money + tList[0].rewardNum;
                  that.setData({
                    money: app.globalData.money,
                  })
                  that.finishToast(tList[0].rewardNum);
                  oldDay0 = newDay0;
                  wx.setStorageSync('oldDay0', oldDay0);
                } else {

                }
              } else {
              that.finishToast(tList[0].rewardNum);
              oldDay0 = newDay0;
              app.globalData.money = money + tList[0].rewardNum;
                  that.setData({
                    money: app.globalData.money,
                  })
              wx.setStorageSync('oldDay0', oldDay0);
              }
            }
            if(tList[1].isEnd ===2) {
              var newDay1 = new Date().toLocaleDateString();
              var oldDay1 = wx.getStorageSync('oldDay1',oldDay1);
              if(oldDay1) {
                if(newDay1 != oldDay1) {
                  app.globalData.money = money + tList[1].rewardNum;
                  that.setData({
                    money: app.globalData.money,
                  })
                  that.finishToast(tList[1].rewardNum);
                  oldDay1 = newDay1;
                  wx.setStorageSync('oldDay1', oldDay1);
                } else {

                }
              } else {
                app.globalData.money = money + tList[1].rewardNum;
                  that.setData({
                    money: app.globalData.money,
                  })
              that.finishToast(tList[1].rewardNum);
              oldDay1 = newDay1;
              wx.setStorageSync('oldDay1', oldDay1);
              }
            }
            if(tList[2].isEnd ===2) {
              var newDay2 = new Date().toLocaleDateString();
              var oldDay2 = wx.getStorageSync('oldDay2',oldDay2);
              if(oldDay2) {
                if(newDay2 != oldDay2) {
                  app.globalData.money = money + tList[2].rewardNum;
                  that.setData({
                    money: app.globalData.money,
                  })
                  that.finishToast(tList[2].rewardNum);
                  oldDay2 = newDay2;
                  wx.setStorageSync('oldDay2', oldDay2);
                } else {

                }
              } else {
                app.globalData.money = money + tList[2].rewardNum;
                  that.setData({
                    money: app.globalData.money,
                  })
              that.finishToast(tList[2].rewardNum);
              oldDay2 = newDay2;
              wx.setStorageSync('oldDay2', oldDay2);
              }
            }
            if(tList[3].isEnd ===2) {
              var newDay3 = new Date().toLocaleDateString();
              var oldDay3 = wx.getStorageSync('oldDay3',oldDay3);
              if(oldDay3) {
                if(newDay3 != oldDay3) {
                  app.globalData.money = money + tList[3].rewardNum;
                  that.setData({
                    money: app.globalData.money,
                  })
                  that.finishToast(tList[3].rewardNum);
                  oldDay3 = newDay3;
                  wx.setStorageSync('oldDay3', oldDay3);
                } else {

                }
              } else {
                app.globalData.money = money + tList[3].rewardNum;
                  that.setData({
                    money: app.globalData.money,
                  })
              that.finishToast(tList[3].rewardNum);
              oldDay3 = newDay3;
              wx.setStorageSync('oldDay3', oldDay3);
              }
            }
        }
      }
          })
  },
  // 开启弹窗：
  finishToast: function(a) {
    var that = this;
    var money = that.data.money;
    that.setData({
      showMask: true,
      fourShow: true,
      rewardNum: a,
    })
    var userMoney = Math.floor(money / 10000 * 100) / 100;
    that.setData({
      userMoney: userMoney,
    })
  },
  // 关闭弹窗
  clickclosefour: function () {
    var that = this;
    var isDelay = that.data.isDelay
    if(isDelay == 1) {
      var delayTimer = setInterval(function () {
        that.setData({
          showMask: false,
          fourShow: false,
        })
        clearInterval(delayTimer);
      },2000)
    } else {
      that.setData({
        showMask: false,
        fourShow: false,
      })
    }
  },
   // 关闭banner广告
   bannerClose: function () {
     var that = this;
     that.setData({
      bannerShow: false,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})